/**
 * 统一API请求工具类
 * 整合Ajax、Fetch、Axios三种请求方式，提供统一的API接口
 */

// 导入各种请求方式
export { AjaxClient, AjaxRequest, AjaxConfig, AjaxResponse, AjaxError, createAjaxClient, ajax } from './ajax';
export { FetchClient, FetchRequest, FetchConfig, FetchResponse, FetchError, createFetchClient, fetch } from './fetch';
export { AxiosClient, AxiosRequest, AxiosConfig, AxiosResponse, AxiosError, AxiosInterceptor, AxiosTemplate, PageParams, PageResponse, COMMON_TEMPLATES, createAxiosClient, axios } from './axios';

/**
 * 请求方式枚举
 */
export enum RequestType {
  AJAX = 'ajax',
  FETCH = 'fetch',
  AXIOS = 'axios'
}

/**
 * 统一请求配置接口
 */
export interface UnifiedRequestConfig {
  type?: RequestType;
  url: string;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
  headers?: Record<string, string>;
  data?: any;
  params?: Record<string, any>;
  timeout?: number;
  withCredentials?: boolean;
  responseType?: 'json' | 'text' | 'blob' | 'arraybuffer' | 'document';
  onUploadProgress?: (progress: number) => void;
  onDownloadProgress?: (progress: number) => void;
}

/**
 * 统一响应数据接口
 */
export interface UnifiedResponse<T = any> {
  data: T;
  status: number;
  statusText: string;
  headers: Record<string, string>;
  config: UnifiedRequestConfig;
}

/**
 * 统一错误信息接口
 */
export interface UnifiedError {
  message: string;
  status?: number;
  statusText?: string;
  data?: any;
  config?: UnifiedRequestConfig;
}

/**
 * 统一API客户端类
 */
export class UnifiedApiClient {
  private defaultType: RequestType;
  private ajaxClient: any;
  private fetchClient: any;
  private axiosClient: any;

  constructor(config: {
    defaultType?: RequestType;
    baseURL?: string;
    timeout?: number;
    headers?: Record<string, string>;
    withCredentials?: boolean;
  } = {}) {
    this.defaultType = config.defaultType || RequestType.AXIOS;
    
    // 初始化各种客户端
    this.ajaxClient = new (require('./ajax').AjaxClient)(config);
    this.fetchClient = new (require('./fetch').FetchClient)(config);
    this.axiosClient = new (require('./axios').AxiosClient)(config);
  }

  /**
   * 发送请求
   */
  async request<T = any>(config: UnifiedRequestConfig): Promise<UnifiedResponse<T>> {
    const requestType = config.type || this.defaultType;
    
    switch (requestType) {
      case RequestType.AJAX:
        return this.ajaxClient.request<T>(config);
      case RequestType.FETCH:
        return this.fetchClient.request<T>(config);
      case RequestType.AXIOS:
        return this.axiosClient.request<T>(config);
      default:
        throw new Error(`Unsupported request type: ${requestType}`);
    }
  }

  /**
   * GET请求
   */
  async get<T = any>(url: string, params?: Record<string, any>, config?: Partial<UnifiedRequestConfig>): Promise<UnifiedResponse<T>> {
    return this.request<T>({
      ...config,
      url,
      method: 'GET',
      params
    });
  }

  /**
   * POST请求
   */
  async post<T = any>(url: string, data?: any, config?: Partial<UnifiedRequestConfig>): Promise<UnifiedResponse<T>> {
    return this.request<T>({
      ...config,
      url,
      method: 'POST',
      data
    });
  }

  /**
   * PUT请求
   */
  async put<T = any>(url: string, data?: any, config?: Partial<UnifiedRequestConfig>): Promise<UnifiedResponse<T>> {
    return this.request<T>({
      ...config,
      url,
      method: 'PUT',
      data
    });
  }

  /**
   * DELETE请求
   */
  async delete<T = any>(url: string, config?: Partial<UnifiedRequestConfig>): Promise<UnifiedResponse<T>> {
    return this.request<T>({
      ...config,
      url,
      method: 'DELETE'
    });
  }

  /**
   * PATCH请求
   */
  async patch<T = any>(url: string, data?: any, config?: Partial<UnifiedRequestConfig>): Promise<UnifiedResponse<T>> {
    return this.request<T>({
      ...config,
      url,
      method: 'PATCH',
      data
    });
  }
}

/**
 * 创建统一API客户端
 */
export function createUnifiedApiClient(config?: {
  defaultType?: RequestType;
  baseURL?: string;
  timeout?: number;
  headers?: Record<string, string>;
  withCredentials?: boolean;
}): UnifiedApiClient {
  return new UnifiedApiClient(config);
}

/**
 * 快速请求方法
 */
export const api = {
  /**
   * 创建统一API客户端
   */
  createClient: createUnifiedApiClient,
  
  /**
   * 快速GET请求
   */
  get: async <T = any>(url: string, params?: Record<string, any>, type?: RequestType): Promise<UnifiedResponse<T>> => {
    const client = createUnifiedApiClient();
    return client.get<T>(url, params, { type });
  },
  
  /**
   * 快速POST请求
   */
  post: async <T = any>(url: string, data?: any, type?: RequestType): Promise<UnifiedResponse<T>> => {
    const client = createUnifiedApiClient();
    return client.post<T>(url, data, { type });
  },
  
  /**
   * 快速PUT请求
   */
  put: async <T = any>(url: string, data?: any, type?: RequestType): Promise<UnifiedResponse<T>> => {
    const client = createUnifiedApiClient();
    return client.put<T>(url, data, { type });
  },
  
  /**
   * 快速DELETE请求
   */
  delete: async <T = any>(url: string, type?: RequestType): Promise<UnifiedResponse<T>> => {
    const client = createUnifiedApiClient();
    return client.delete<T>(url, { type });
  }
};